//
//  LoginLogic.swift
//  TTBLL
//
//  Created by Toj on 5/12/21.
//

import Foundation

open class LoginLogic: NSObject, TTPersistence {
    
    public func secret(completion: @escaping ((_ appSecret: String)->())) {
        TTAPI.Request.secret { response in
            
            guard let appSecret = response.data else {
                // alert
                return
            }
            completion(appSecret)
        }
    }
    
    public func login(parameters: [String : Any]?, completion: @escaping ((_ login: TTLoginModel)->())) {
        secret { [weak self] (appSecret) in
            
            print(appSecret)
            self?.save(appSecret: appSecret)
            
            TTAPI.Request.login(parameters, completion: { response in
                guard let loginInfo = response.data else {
                    // alert
                    return
                }
                self?.save(model: loginInfo)
                completion(loginInfo)
            })
        }
    }
    
    public func loginTitle() -> String {
        return "TT".tt_localized
    }
}
